﻿using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using SystemCenterSoa.ApiLibrary.PermissionsValidation;
using SystemCenterSoa.ApiProject.Area.SystemManager;
using SystemCenterSoa.DtoModel.Common;
using SystemCenterSoa.PoModel.Attendance_Management;
using SystemCenterSoa.Service.Attendance_Management;
using SystemCenterSoa.Service.SystemManager;
using System.Data.SqlClient;
using Dapper;
using Microsoft.AspNetCore.Authorization;
using SystemCenterSoa.PoModel.SystemSetup.InstitutionSetting.PeopleManagement;
using SystemCenterSoa.DtoModel.Attendance_Management;
using NPOI.HSSF.UserModel;
using Microsoft.AspNetCore.Hosting;
using System.IO;

namespace SystemCenterSoa.ApiProject.Area.Attendance_Management
{
    [ApiModuleRelation("attendance_management")]
    [AllowAnonymous]
    public class Attendance_ManagementController : SystemManagerBaseController
    {

        private AttendanceManagementService _attendanceManagementService;
        private  IHostingEnvironment _hostingEnvironment;
        public Attendance_ManagementController(AttendanceManagementService attendanceManagementService, IHostingEnvironment hostingEnvironment)
        {
            _attendanceManagementService = attendanceManagementService;
            _hostingEnvironment = hostingEnvironment;
        }

        [HttpPost]
        public ResultModel<int> AskLeavePoAdd(ApplyLeavePo oneForm)
        {
            string loginname = LoginUser.user_name;
            string sql = "select people_management_department_name from people_management where people_management_loginname=@loginname";
            using(SqlConnection conn=new SqlConnection(Config))
            {
                conn.Open();
                var a=conn.Query<string>(sql,new { @loginname=loginname}).ToList();
                oneForm.subordinate_departments =a[0] ; 
            }
            oneForm.create_time = oneForm.create_time.AddDays(1);
            oneForm.end_time = oneForm.end_time.AddDays(1);
            oneForm.creator_name = LoginUser.user_full_name;
            var data = _attendanceManagementService.AskLeavePoAdd(oneForm);
            return MyOk(data);
        }

        [HttpPost]

        public ResultModel<int> BusinessTravelRequestPoAdd(BusinessTravelRequestPo twoForm)
        {
            string loginname = LoginUser.user_name;
            string sql = "select people_management_department_name from people_management where people_management_loginname=@loginname";
            using (SqlConnection conn = new SqlConnection(Config))
            {
                conn.Open();
                var a = conn.Query<string>(sql, new { @loginname = loginname }).ToList();
                twoForm.subordinate_departments = a[0];
            }
            twoForm.create_time = twoForm.create_time.AddDays(1);
            twoForm.end_time = twoForm.end_time.AddDays(1);
            twoForm.creator_name = LoginUser.user_full_name;
            var data = _attendanceManagementService.BusinessTravelRequestPoAdd(twoForm);
            return MyOk(data);
        }

        [HttpPost]

        public ResultModel<int> FieldApplicationPoAdd(FieldApplicationPo threeForm)
        {
            string loginname = LoginUser.user_name;
            string sql = "select people_management_department_name from people_management where people_management_loginname=@loginname";
            using (SqlConnection conn = new SqlConnection(Config))
            {
                conn.Open();
                var a = conn.Query<string>(sql, new { @loginname = loginname }).ToList();
                threeForm.subordinate_departments = a[0];
            }
            threeForm.create_time = threeForm.create_time.AddDays(1);
            threeForm.end_time = threeForm.end_time.AddDays(1);
            threeForm.creator_name = LoginUser.user_full_name;
            var data = _attendanceManagementService.FieldApplicationPoAdd(threeForm);
            return MyOk(data);
        }

        [HttpPost]

        public ResultModel<int> NotClockPoAdd(NotClockPo fourForm)
        {
            string loginname = LoginUser.user_name;
            string sql = "select people_management_department_name from people_management where people_management_loginname=@loginname";
            using (SqlConnection conn = new SqlConnection(Config))
            {
                conn.Open();
                var a = conn.Query<string>(sql, new { @loginname = loginname }).ToList();
                fourForm.subordinate_departments = a[0];
            }
            fourForm.create_time = fourForm.create_time.AddDays(1);
            fourForm.end_time = fourForm.end_time.AddDays(1);
            fourForm.creator_name = LoginUser.user_full_name;
            var data = _attendanceManagementService.NotClockPoAdd(fourForm);
            return MyOk(data);
        }

        [HttpPost]

        public ResultModel<int> CompanyActivitiesPoAdd(CompanyActivitiesPo fiveForm)
        {
            string loginname = LoginUser.user_name;
            string sql = "select people_management_department_name from people_management where people_management_loginname=@loginname";
            using (SqlConnection conn = new SqlConnection(Config))
            {
                conn.Open();
                var a = conn.Query<string>(sql, new { @loginname = loginname }).ToList();
                fiveForm.subordinate_departments = a[0];
            }
            fiveForm.create_time = fiveForm.create_time.AddDays(1);
            fiveForm.end_time = fiveForm.end_time.AddDays(1);
            fiveForm.creator_name = LoginUser.user_full_name;
            var data = _attendanceManagementService.CompanyActivitiesPoAdd(fiveForm);
            return MyOk(data);
        }


        [HttpPost]

        public ResultModel<int> DepartmentActivityPoAdd(DepartmentActivityPo sixForm)
        {
            string loginname = LoginUser.user_name;
            string sql = "select people_management_department_name from people_management where people_management_loginname=@loginname";
            using (SqlConnection conn = new SqlConnection(Config))
            {
                conn.Open();
                var a = conn.Query<string>(sql, new { @loginname = loginname }).ToList();
                sixForm.subordinate_departments = a[0];
            }
            sixForm.create_time = sixForm.create_time.AddDays(1);
            sixForm.end_time = sixForm.end_time.AddDays(1);
            sixForm.creator_name = LoginUser.user_full_name;
            var data = _attendanceManagementService.DepartmentActivityPoAdd(sixForm);
            return MyOk(data);
        }


        [HttpPost]

        public ResultModel<int> WorkOvertimeLeaveAdd(WorkOvertimeLeavePo sevenForm)
        {
            string loginname = LoginUser.user_name;
            string sql = "select people_management_department_name from people_management where people_management_loginname=@loginname";
            using (SqlConnection conn = new SqlConnection(Config))
            {
                conn.Open();
                var a = conn.Query<string>(sql, new { @loginname = loginname }).ToList();
                sevenForm.subordinate_departments = a[0];
            }
            sevenForm.create_time = sevenForm.create_time.AddDays(1);
            sevenForm.end_time = sevenForm.end_time.AddDays(1);
            sevenForm.creator_name = LoginUser.user_full_name;
            var data = _attendanceManagementService.WorkOvertimeLeaveAdd(sevenForm);
            return MyOk(data);
        }

        [HttpGet]
        public ResultModel<object> GetAllApplicationType()
        {
            var data = _attendanceManagementService.GetAllApplicationType();
            return MyOk(data);
        }

        [HttpGet]
        public ResultModel<ApplyLeavePo> Edit(int id)
        {
            var data = _attendanceManagementService.Edit(id);
            return MyOk(data);
        }


        [HttpGet]
        public ResultModel<BusinessTravelRequestPo> TwoEdit(int id)
        {
            var data = _attendanceManagementService.TwoEdit(id);
            return MyOk(data);
        }


        [HttpGet]
        public ResultModel<FieldApplicationPo> ThreeEdit(int id)
        {
            var data = _attendanceManagementService.ThreeEdit(id);
            return MyOk(data);
        }


        [HttpGet]
        public ResultModel<NotClockPo> FourEdit(int id)
        {
            var data = _attendanceManagementService.FourEdit(id);
            return MyOk(data);
        }


        [HttpGet]
        public ResultModel<CompanyActivitiesPo> FiveEdit(int id)
        {
            var data = _attendanceManagementService.FiveEdit(id);
            return MyOk(data);
        }

        [HttpGet]
        public ResultModel<DepartmentActivityPo> SixEdit(int id)
        {
            var data = _attendanceManagementService.SixEdit(id);
            return MyOk(data);
        }
        [HttpGet]
        public ResultModel<WorkOvertimeLeavePo> SevenEdit(int id)
        {
            var data = _attendanceManagementService.SevenEdit(id);
            return MyOk(data);
        }


        [HttpGet]
        public ResultModel<object> GetAllAskLeave(string depname, int year, int month, int pageIndex, int pageSize)
        {
            var data = _attendanceManagementService.GetAllAskLeave(depname, year, month, pageIndex, pageSize);
            return MyOk(data);
        }


        [HttpGet]
        public ResultModel<object> GetAllBusinessTravelRequestPo(string depname, int year, int month, int pageIndex, int pageSize)
        {
            var data = _attendanceManagementService.GetAllBusinessTravelRequestPo(depname, year, month, pageIndex, pageSize);
            return MyOk(data);
        }


        [HttpGet]
        public ResultModel<object> GetAllFieldApplicationPo(string depname, int year, int month, int pageIndex, int pageSize)
        {
            var data = _attendanceManagementService.GetAllFieldApplicationPo(depname, year, month, pageIndex, pageSize);
            return MyOk(data);
        }

        [HttpGet]
        public ResultModel<object> GetAllNotClockPo(string depname, int year, int month, int pageIndex, int pageSize)
        {
            var data = _attendanceManagementService.GetAllNotClockPo(depname, year, month, pageIndex, pageSize);
            return MyOk(data);
        }

        [HttpGet]
        public ResultModel<object> GetAllCompanyActivitiesPo(string depname, int year, int month, int pageIndex, int pageSize)
        {
            var data = _attendanceManagementService.GetAllCompanyActivitiesPo(depname, year, month, pageIndex, pageSize);
            return MyOk(data);
        }

        [HttpGet]
        public ResultModel<object> GetAllDepartmentActivityPo(string depname, int year, int month, int pageIndex, int pageSize)
        {
            var data = _attendanceManagementService.GetAllDepartmentActivityPo(depname, year, month, pageIndex, pageSize);
            return MyOk(data);
        }

        [HttpGet]
        public ResultModel<object> GetAllWorkOvertimeLeavePo(string depname, int year, int month, int pageIndex, int pageSize)
        {
            var data = _attendanceManagementService.GetAllWorkOvertimeLeavePo(depname, year, month, pageIndex, pageSize);
            return MyOk(data);
        }

        //修改状态
        [HttpPost]
        public ResultModel<int> AskLeaveAudit(int status, int id)
        {
            string modifi_name = LoginUser.user_full_name;
            var data = _attendanceManagementService.AskLeaveAudit(status, id, modifi_name);
            return MyOk(data);
        }


        [HttpPost]
        public ResultModel<int> BusinessTravelRequestPoAudit(int status, int id)
        {
            string modifi_name = LoginUser.user_full_name;
            var data = _attendanceManagementService.BusinessTravelRequestPoAudit(status, id, modifi_name);
            return MyOk(data);
        }

        [HttpPost]
        public ResultModel<int> FieldApplicationPoAudit(int status, int id)
        {
            string modifi_name = LoginUser.user_full_name;
            var data = _attendanceManagementService.FieldApplicationPoAudit(status, id, modifi_name);
            return MyOk(data);
        }


        [HttpPost]
        public ResultModel<int> NotClockPoAudit(int status, int id)
        {
            string modifi_name = LoginUser.user_full_name;
            var data = _attendanceManagementService.NotClockPoAudit(status, id, modifi_name);
            return MyOk(data);
        }

        [HttpPost]
        public ResultModel<int> CompanyActivitiesPoAudit(int status, int id)
        {
            string modifi_name = LoginUser.user_full_name;
            var data = _attendanceManagementService.CompanyActivitiesPoAudit(status, id, modifi_name);
            return MyOk(data);
        }


        [HttpPost]
        public ResultModel<int> DepartmentActivityPoAudit(int status, int id)
        {
            string modifi_name = LoginUser.user_full_name;
            var data = _attendanceManagementService.DepartmentActivityPoAudit(status, id, modifi_name);
            return MyOk(data);
        }

        [HttpPost]
        public ResultModel<int> WorkOvertimeLeavePoAudit(int status, int id)
        {
            string modifi_name = LoginUser.user_full_name;
            var data = _attendanceManagementService.WorkOvertimeLeavePoAudit(status, id, modifi_name);
            return MyOk(data);
        }

        //删除-----------------------------------
        [HttpPost]
        public ResultModel<int> AskLeaveDelete(List<int> ids)
        {
            var data = _attendanceManagementService.AskLeaveDelete(ids);
            return MyOk(data);
        }

        [HttpPost]
        public ResultModel<int> BusinessTravelRequestPoDelete(List<int> ids)
        {
            var data = _attendanceManagementService.BusinessTravelRequestPoDelete(ids);
            return MyOk(data);
        }

        [HttpPost]
        public ResultModel<int> FieldApplicationPoDelete(List<int> ids)
        {
            var data = _attendanceManagementService.FieldApplicationPoDelete(ids);
            return MyOk(data);
        }

        [HttpPost]
        public ResultModel<int> NotClockPoDelete(List<int> ids)
        {
            var data = _attendanceManagementService.NotClockPoDelete(ids);
            return MyOk(data);
        }

        [HttpPost]
        public ResultModel<int> CompanyActivitiesPoDelete(List<int> ids)
        {
            var data = _attendanceManagementService.CompanyActivitiesPoDelete(ids);
            return MyOk(data);
        }

        [HttpPost]
        public ResultModel<int> DepartmentActivityPoDelete(List<int> ids)
        {
            var data = _attendanceManagementService.DepartmentActivityPoDelete(ids);
            return MyOk(data);
        }

        [HttpPost]
        public ResultModel<int> WorkOvertimeLeavePoDelete(List<int> ids)
        {
            var data = _attendanceManagementService.WorkOvertimeLeavePoDelete(ids);
            return MyOk(data);
        }

        //----------------------------------------请假列表下拉  部门
        [HttpGet]

        public ResultModel<List<string>> GetAllDepartment()
        {
            string sql = "select  dep_name from divisional_management ";
            using (SqlConnection conn = new SqlConnection(Config))
            {
                conn.Open();
                List<string> data = conn.Query<string>(sql).ToList();
                return MyOk(data);
            }
        }
        //考勤统计
        [HttpGet]
        public ResultModel<object> AttendanceStatistics(string depname, string name, int pageIndex, int pageSize)
        {
            string sql = " select *from people_management ";
            using (SqlConnection conn = new SqlConnection(Config))
            {
                conn.Open();
                var list = conn.Query<PeoplePo>(sql).ToList();
                int totalCount = list.Count();
                if(!string.IsNullOrWhiteSpace(depname))
                {
                    list = list.Where(c => c.people_management_department_name == depname).ToList();
                }
                if (!string.IsNullOrWhiteSpace(name))
                {
                    list = list.Where(c => c.people_management_name.Contains(name)).ToList();
                }
                list=list.OrderBy(c => c.people_management_id).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
                object data = new { list, totalCount };
                return MyOk(data);
            }
        }
        //考勤申请修改
        [HttpPost]

        public ResultModel<int> AskLeavePoUpdate(ApplyLeavePo form)
        {
            var data = _attendanceManagementService.AskLeavePoUpdate(form);
            return MyOk(data);
        }

        [HttpPost]

        public ResultModel<int> BusinessTravelRequestPoUpdate(BusinessTravelRequestPo form)
        {
            var data = _attendanceManagementService.BusinessTravelRequestPoUpdate(form);
            return MyOk(data);
        }

        [HttpPost]

        public ResultModel<int> FieldApplicationPoUpdate(FieldApplicationPo form)
        {
            var data = _attendanceManagementService.FieldApplicationPoUpdate(form);
            return MyOk(data);
        }

        [HttpPost]

        public ResultModel<int> NotClockPoUpdate(NotClockPo form)
        {
            var data = _attendanceManagementService.NotClockPoUpdate(form);
            return MyOk(data);
        }

        [HttpPost]

        public ResultModel<int> CompanyActivitiesPoUpdate(CompanyActivitiesPo form)
        {
            var data = _attendanceManagementService.CompanyActivitiesPoUpdate(form);
            return MyOk(data);
        }

        [HttpPost]

        public ResultModel<int> DepartmentActivityPUpdate(DepartmentActivityPo form)
        {
            var data = _attendanceManagementService.DepartmentActivityPUpdate(form);
            return MyOk(data);
        }

        [HttpPost]

        public ResultModel<int> WorkOvertimeLeaveUpdate(WorkOvertimeLeavePo form)
        {
            var data = _attendanceManagementService.WorkOvertimeLeaveUpdate(form);
            return MyOk(data);
        }

        [HttpGet]
        public ResultModel<List<AnnualLeaveStatisticsDto>> GetAllAnnualLeaveStatisticsDto(string depname, string name, int year)
        {
            var data = _attendanceManagementService.GetAllAnnualLeaveStatisticsDto(depname, name, year);
            return MyOk(data);
        }

        //年假统计  人员下拉
        [HttpGet]

        public ResultModel<List<string>> GetPersonnel(string depname)
        {
            string sql = "select people_management_name from people_management where people_management_department_name=@depname";
            using(SqlConnection conn=new SqlConnection(Config))
                {
                conn.Open();
                var data=conn.Query<string>(sql, new { @depname = depname }).ToList();
                return MyOk(data);
                }

        }

        ///年假统计 导出Excel
        [HttpGet]
        public ResultModel<string> GetContractExcel(string depname, string name, int year)
        {
            try
            {
                //定义工作簿
                HSSFWorkbook workbook = new HSSFWorkbook();
                //创建Sheet表单
                HSSFSheet sheet = (HSSFSheet)workbook.CreateSheet();
                //设置表单列的宽度
                sheet.DefaultColumnWidth = 20;

                //新建标题行
                HSSFRow dataRow = (HSSFRow)sheet.CreateRow(0);
                dataRow.CreateCell(0).SetCellValue("部门名称");
                dataRow.CreateCell(1).SetCellValue("序号");
                dataRow.CreateCell(2).SetCellValue("姓名");
                dataRow.CreateCell(3).SetCellValue("今年(天)");
                dataRow.CreateCell(4).SetCellValue("今年(小时)");
                dataRow.CreateCell(5).SetCellValue("去年剩余(小时)");
                dataRow.CreateCell(6).SetCellValue("12.16至12.31");
                dataRow.CreateCell(7).SetCellValue("1.1至1.15");
                dataRow.CreateCell(8).SetCellValue("1.16至2.15");
                dataRow.CreateCell(9).SetCellValue("2.16至2.28");
                dataRow.CreateCell(10).SetCellValue("3.1至3.15");
                dataRow.CreateCell(11).SetCellValue("3.16至4.15");
                dataRow.CreateCell(12).SetCellValue("4.16至5.15");
                dataRow.CreateCell(13).SetCellValue("5.16至6.15");
                dataRow.CreateCell(14).SetCellValue("6.16至7.15");
                dataRow.CreateCell(15).SetCellValue("7.16至8.15");
                dataRow.CreateCell(16).SetCellValue("8.16至9.15");
                dataRow.CreateCell(17).SetCellValue("9.16至10.15");
                dataRow.CreateCell(18).SetCellValue("10.16至11.15");
                dataRow.CreateCell(19).SetCellValue("11.16至12.15");
                dataRow.CreateCell(20).SetCellValue("剩余小时");

                var row = 1;
                // var lsit = _contractRepository.GetContractExcel();
                var list = _attendanceManagementService.GetAllAnnualLeaveStatisticsDto(depname, name, year);
                var datas = Newtonsoft.Json.JsonConvert.SerializeObject(list);
                var persons = Newtonsoft.Json.JsonConvert.DeserializeObject<List<AnnualLeaveStatisticsDto>>(datas);
                list.ForEach(m =>
                {
                    dataRow = (HSSFRow)sheet.CreateRow(row);//新建数据行
                    dataRow.CreateCell(0).SetCellValue(m.depname);
                    dataRow.CreateCell(1).SetCellValue(m.annual_leave_id);
                    dataRow.CreateCell(2).SetCellValue(m.name);
                    dataRow.CreateCell(3).SetCellValue(Convert.ToDouble(m.this_year_day));
                    dataRow.CreateCell(4).SetCellValue(Convert.ToDouble(m.this_year_hour));
                    dataRow.CreateCell(5).SetCellValue(Convert.ToDouble(m.last_year_hour));
                    dataRow.CreateCell(6).SetCellValue(Convert.ToDouble(m.one));
                    dataRow.CreateCell(7).SetCellValue(Convert.ToDouble(m.two));
                    dataRow.CreateCell(8).SetCellValue(Convert.ToDouble(m.three));
                    dataRow.CreateCell(9).SetCellValue(Convert.ToDouble(m.four));
                    dataRow.CreateCell(10).SetCellValue(Convert.ToDouble(m.five));
                    dataRow.CreateCell(11).SetCellValue(Convert.ToDouble(m.six));
                    dataRow.CreateCell(12).SetCellValue(Convert.ToDouble(m.seven));
                    dataRow.CreateCell(13).SetCellValue(Convert.ToDouble(m.eight));
                    dataRow.CreateCell(14).SetCellValue(Convert.ToDouble(m.nine));
                    dataRow.CreateCell(15).SetCellValue(Convert.ToDouble(m.ten));
                    dataRow.CreateCell(16).SetCellValue(Convert.ToDouble(m.eleven));
                    dataRow.CreateCell(17).SetCellValue(Convert.ToDouble(m.twelve));
                    dataRow.CreateCell(18).SetCellValue(Convert.ToDouble(m.thirteen));
                    dataRow.CreateCell(19).SetCellValue(Convert.ToDouble(m.fourteen));
                    dataRow.CreateCell(20).SetCellValue(Convert.ToDouble(m.year_remaining_hour));
                    row++;
                });

                var path = _hostingEnvironment.ContentRootPath + "/excel/temp/" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls";
                using (FileStream fs = new FileStream(path, FileMode.Create, FileAccess.Write))
                {
                    workbook.Write(fs);
                }
            }
            catch
            {
                return MyOk("导出失败！");
            }
            return MyOk("导出成功！");
        }


        //部门加班费管理
        [HttpGet]
        public ResultModel<List<DepartmentOvertimeManagementPo>> GetDepartmentOvertimeManagementPo(string depname, int year, int month)
        {
            var data = _attendanceManagementService.GetDepartmentOvertimeManagementPo(depname, year, month);
            return MyOk(data);
        }

        //部门加班费管理导出Excel
        [HttpGet]
        public ResultModel<string> GetDepartmentOvertimeExcel(string depname, int year, int month)
        {
            try
            {
                //定义工作簿
                HSSFWorkbook workbook = new HSSFWorkbook();
                //创建Sheet表单
                HSSFSheet sheet = (HSSFSheet)workbook.CreateSheet();
                //设置表单列的宽度
                sheet.DefaultColumnWidth = 20;

                //新建标题行
                HSSFRow dataRow = (HSSFRow)sheet.CreateRow(0);
                dataRow.CreateCell(0).SetCellValue("部门名称");
                dataRow.CreateCell(1).SetCellValue("序号");
                dataRow.CreateCell(2).SetCellValue("姓名");
                dataRow.CreateCell(3).SetCellValue("月工资");
                dataRow.CreateCell(4).SetCellValue("请假(h)");
                dataRow.CreateCell(5).SetCellValue("迟到(次)");
                dataRow.CreateCell(6).SetCellValue("加班(h)");
                dataRow.CreateCell(7).SetCellValue("系数");
                dataRow.CreateCell(8).SetCellValue("计算额");
                dataRow.CreateCell(9).SetCellValue("调配额");
               
                var row = 1;
                // var lsit = _contractRepository.GetContractExcel();
                var list = _attendanceManagementService.GetDepartmentOvertimeManagementPo(depname,year,month);
                var datas = Newtonsoft.Json.JsonConvert.SerializeObject(list);
                var persons = Newtonsoft.Json.JsonConvert.DeserializeObject<List<AnnualLeaveStatisticsDto>>(datas);
                list.ForEach(m =>
                {
                    dataRow = (HSSFRow)sheet.CreateRow(row);//新建数据行
                    dataRow.CreateCell(0).SetCellValue(m.depname);
                    dataRow.CreateCell(1).SetCellValue(m.id);
                    dataRow.CreateCell(2).SetCellValue(m.staff_name);
                    dataRow.CreateCell(3).SetCellValue(Convert.ToDouble(m.monthly_salary));
                    dataRow.CreateCell(4).SetCellValue(Convert.ToDouble(m.ask_leave));
                    dataRow.CreateCell(5).SetCellValue(Convert.ToDouble(m.late));
                    dataRow.CreateCell(6).SetCellValue(Convert.ToDouble(m.work_overtime));
                    dataRow.CreateCell(7).SetCellValue(Convert.ToDouble(m.coefficient));
                    dataRow.CreateCell(8).SetCellValue(Convert.ToDouble(m.calculate_the_forehead));
                    dataRow.CreateCell(8).SetCellValue(Convert.ToDouble(m.the_quota));
                    row++;
                });

                var path = _hostingEnvironment.ContentRootPath + "/excel/temp/" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls";
                using (FileStream fs = new FileStream(path, FileMode.Create, FileAccess.Write))
                {
                    workbook.Write(fs);
                }
            }
            catch
            {
                return MyOk("导出失败！");
            }
            return MyOk("导出成功！");
        }

        //员工出勤显示
        [HttpGet]
        public ResultModel<List<EmployeeAttendanceSettingsPo>> GetAllEmployeeAttendanceSettingsPo(string depname, int year, string username)
        {
            var data=_attendanceManagementService.GetAllEmployeeAttendanceSettingsPo(depname, year, username);
            return MyOk(data);
        }

        [HttpPost]
        public ResultModel<int> UpdateEmployeeAttendanceSettingsPo(List<EmployeeAttendanceSettingsPo> tabledata)
        {
            var data= _attendanceManagementService.UpdateEmployeeAttendanceSettingsPo(tabledata);
            return MyOk(data);
        }

        //部门经理设置
        [HttpGet]
        public ResultModel<List<DepartmentListPo>> GetDepartmentListPo()
        {
            var data = _attendanceManagementService.GetDepartmentListPo();
            return MyOk(data);
        }


        //节假日设置
        [HttpGet]
        public ResultModel<object> GetHolidaySettingsPo(int year)
        {
            var data=_attendanceManagementService.GetHolidaySettingsPo(year);
            return MyOk(data);
        }
        //节假日
        [HttpPost]
        public ResultModel<int> AddHolidaySeason(DateTime date)
        {
            var data=_attendanceManagementService.AddHolidaySeason(date);
            return MyOk(data);
        }
        [HttpPost]
        public ResultModel<int> DeleteHolidaySettingsPo(int id)
        {
            return MyOk(_attendanceManagementService.DeleteHolidaySettingsPo(id));
        }

        //特殊工作日
        [HttpPost]
        public ResultModel<int> AddSpecialWorkingDay(DateTime date)
        {
            return  MyOk(_attendanceManagementService.AddSpecialWorkingDay(date));
        }
        //年假设置
        [HttpGet]
        public ResultModel<List<AnnualLeaveSetPo>> GetAnnualLeaveSetPo(string depname, int year)
        {
            return MyOk(_attendanceManagementService.GetAnnualLeaveSetPo(depname, year));
        }

        [HttpPost]
        public ResultModel<int> UpdateAnnualLeaveSetPo(List<AnnualLeaveSetPo> tabledata)
        {
            return  MyOk(_attendanceManagementService.UpdateAnnualLeaveSetPo(tabledata));
        }

        //上传考勤
        [HttpGet]
        public ResultModel<List<UploadAttendancePo>> GetUploadAttendancePo(int year, int month)
        {
            return MyOk(_attendanceManagementService.GetUploadAttendancePo(year, month));
        }

        [HttpPost]
        public ResultModel<int> AddUploadAttendancePo(string name)
        {
            return MyOk(_attendanceManagementService.AddUploadAttendancePo(name));
        }

        //获取所有人员名字
        [HttpGet]

        public ResultModel<List<string>> GetName()
        {
            string sql = "select people_management_name from people_management ";
            using(SqlConnection conn=new SqlConnection(Config))
            {
                conn.Open();
                List<string> data = conn.Query<string>(sql).ToList();
                return MyOk(data);
            }
        }
        //合同与项目综合统计
        [HttpGet]
        public ResultModel<List<ComprehensiveStatisticsContractsProjectsPo>> GetComprehensiveStatisticsContractsProjectsPo(string depname, int year)
        {
            var data = _attendanceManagementService.GetComprehensiveStatisticsContractsProjectsPo(depname, year);
            return MyOk(data);
        }

        [HttpGet]
        public ResultModel<string> GetComprehensiveStatisticsContractsProjectsPoExcel(string depname, int year)
        {
            try
            {
                //定义工作簿
                HSSFWorkbook workbook = new HSSFWorkbook();
                //创建Sheet表单
                HSSFSheet sheet = (HSSFSheet)workbook.CreateSheet();
                //设置表单列的宽度
                sheet.DefaultColumnWidth = 20;

                //新建标题行
                HSSFRow dataRow = (HSSFRow)sheet.CreateRow(0);
                dataRow.CreateCell(0).SetCellValue("生产部门");
                dataRow.CreateCell(1).SetCellValue("客户信息");
                dataRow.CreateCell(2).SetCellValue("合同信息");
                dataRow.CreateCell(3).SetCellValue("项目信息");
                dataRow.CreateCell(4).SetCellValue("(未策划项目");
                dataRow.CreateCell(5).SetCellValue("已经策划评审项目");

                var row = 1;
                // var lsit = _contractRepository.GetContractExcel();
                var list = _attendanceManagementService.GetComprehensiveStatisticsContractsProjectsPo(depname,year);
                var datas = Newtonsoft.Json.JsonConvert.SerializeObject(list);
                var persons = Newtonsoft.Json.JsonConvert.DeserializeObject<List<AnnualLeaveStatisticsDto>>(datas);
                list.ForEach(m =>
                {
                    dataRow = (HSSFRow)sheet.CreateRow(row);//新建数据行
                    dataRow.CreateCell(0).SetCellValue(m.depname);
                    dataRow.CreateCell(2).SetCellValue(m.customer_information);
                    dataRow.CreateCell(3).SetCellValue(Convert.ToDouble(m.contract_information));
                    dataRow.CreateCell(4).SetCellValue(Convert.ToDouble(m.project_information));
                    dataRow.CreateCell(5).SetCellValue(Convert.ToDouble(m.unplanned_project));
                    dataRow.CreateCell(6).SetCellValue(Convert.ToDouble(m.review_projects_have_been_planned));
                    row++;
                });

                var path = _hostingEnvironment.ContentRootPath + "/excel/temp/" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls";
                using (FileStream fs = new FileStream(path, FileMode.Create, FileAccess.Write))
                {
                    workbook.Write(fs);
                }
            }
            catch
            {
                return MyOk("导出失败！");
            }
            return MyOk("导出成功！");
        }


    }
}
